// pages/authorization/authorization.js
const app = getApp();
const request = require('../../utils/request.js');
const url = require('../../utils/url.js');
const cookie = require('../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isMobile: 1,
    logo: '',
    code: '',
    checkPrivacy: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getlogo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    wx.login({
      success: (res) => {
        that.setData({
          code: res.code
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getlogo() {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getconfig, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.setData({
          logo: res.data.data
        })
      }
    })
  },

  getCode() {
    var that = this;
    wx.login({
      success: (res) => {
        that.setData({
          code: res.code
        })
      }
    })
  },
  getPhoneNumber: function (e) {
    var that = this;

    console.log(e)
    wx.showLoading({
      title: '请等待',
      mask: false
    })
    const encryptedData = e.detail.encryptedData
    const iv = e.detail.iv
    if (e.detail.errMsg != "getPhoneNumber:fail user deny") {
      this.getMobile(encryptedData, iv)
    } else {
      wx.showToast({
        title: '请重新获取',
        icon: "none"
      })
    }
  },
  getMobile: function (encryptedData, iv) {
    var that = this;
    wx.showLoading({
      title: '加载中'
    });
    let toper = wx.getStorageSync('toper'); //上级业务推荐人
    let param = {
      scoure: 'wxapp',
      iv: iv,
      encryptedData: encryptedData,
      code: that.data.code,
      uid: toper.uid ? toper.uid : 0,
      upcode: toper.code ? toper.code : '',
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
	console.log('加载中');
	console.log(param);
    request.request(url.getwxmobile, param, url.REQUEST_METHOD_POST).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.getCode();
        that.setData({
          isMobile: 0
        });
        wx.showToast({
          title: '请继续点击微信授权进行登录!',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })

  },
  getUserInfo: function () {
    var that = this;

    if (that.data.checkPrivacy == 0) {
      that.selectComponent('#xieyi').init();
      return false;
    }

    wx.getUserProfile({
      desc: "获取你的昵称、头像、地区及性别", // 不写不弹提示框
      success: res => {
        console.log(res)
        wx.showLoading({
          title: '登陆中',
          mask: true
        })
        let param = {
          'iv': res.iv,
          'code': that.data.code,
          'encryptedData': res.encryptedData
        }
        request.request(url.wxlogin, param, url.REQUEST_METHOD_POST).then((res) => {
          wx.hideLoading();
          if (res.data.code == 1) {
            //登录缓存
            cookie.setCookie('userInfo', res.data.data);
            cookie.setCookie('token', res.data.data.token);
            that.setData({
              userInfo: res.data.data,
              token: res.data.data.token
            })

            wx.showToast({
              title: '登录成功',
              icon: 'none',
              duration: 3000,
              
            })
            setTimeout(
              function () {
                wx.navigateBack({
                  delta: 1,
                })
              }, 2000
            )

          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 3000
            })
            return;
          }

        })
      },
      fail: res => {
        //拒绝授权
        wx.showToast({
          title: '您拒绝了授权',
          icon: 'none'
        })
        return;
      }
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1,
    })
  }
})